<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Url extends \Phpcmf\Home\Module
{
    public function url_add() {
        
        if (IS_POST && $this->uid) {
            if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法添加进度条');
			}
			$data = \Phpcmf\Service::L('input')->post('data'); //数据
            /*$cid = \Phpcmf\Service::L('input')->post('cid'); //圈子id
			$id = \Phpcmf\Service::L('input')->post('id'); //帖子id
			$title = \Phpcmf\Service::L('input')->post('title'); //标题
			$url = \Phpcmf\Service::L('input')->post('url'); //链接数据*/
            // 判断
			$url_data = \Phpcmf\Service::M()->db->table('1_quan_form_url_data')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->countAllResults();
			
            if($url_data){
				// 插入数据
				$urldata = dr_string2array($data['url']);
				$url_data_list = [];
					foreach ($urldata as $key => $value) {
						$url_data_list[$key]['name'] = $value['name'];
						$url_data_list[$key]['url'] = $value['url'];
					}
                //数据已经存在，那么就修改
                $up_data = [
					'url' => dr_array2string($url_data_list),
				];
				\Phpcmf\Service::M()->db->table('1_quan_form_url_data')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->update($up_data);
				
				$data = [
					'code' => 1,
					'data' => '修改成功',
					'uid'=> 0,
				
				];
            } else {
                //没有数据存在，那么就添加
                // 插入数据
                $urldata = dr_string2array($data['url']);
                $url_data_list = [];
                	foreach ($urldata as $key => $value) {
            			$url_data_list[$key]['name'] = $value['name'];
            			$url_data_list[$key]['url'] = $value['url'];
            		}
				$insert_data = [
					'uid' => $this->uid,
					'quanid' => $data['quanid'],
					'tieid' => $data['tieid'],
					'title' => $data['title'],
					'beizhu' => '',
					'zhuangtai' => 1,
					'tianjiashijian' => time(),
					'url' => dr_array2string($url_data_list),
				];
				\Phpcmf\Service::M()->db->table('1_quan_form_url_data')->insert($insert_data);
				
				$data = [
					'code' => 1,
					'data' => '添加成功',
					'uid'=> 0,
				
				];
                
                
            }
            
            $this->_json('', '操作成功',$data);
        } else {
            
            $data = [
				'code' => 0,
				'data' => '无效提交',
				'uid'=> 0,
			
			];
			$this->_json('0', '无效提交');
		}
       
    }
    //展示
	public function url_data_list() {
		$id = \Phpcmf\Service::L('input')->get('id'); //帖子id
		$cid = \Phpcmf\Service::L('input')->get('cid'); //圈子id
		$url_data_list = \Phpcmf\Service::M()->db->table('1_quan_form_url_data')->where('tieid', $id)->where('quanid', $cid)->get()->getRowArray();
		//循环 $url_data_list 
		$urldata = dr_string2array($url_data_list['url']);
		$url_data_list = [];
		foreach ($urldata as $key => $value) {
			$url_data_list[$key]['name'] = $value['name'];
			$url_data_list[$key]['url'] = $value['url'];
			
		}
		$this->_json('1', '读取数据成功', $url_data_list);
	}
	//删除url
	public function url_data_del() {
		if (IS_POST && $this->uid) {
			if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法删除');
			}
			$cid = \Phpcmf\Service::L('input')->post('cid'); //圈子id
			$id = \Phpcmf\Service::L('input')->post('id'); //帖子id
			// 判断
			$url_data = \Phpcmf\Service::M()->db->table('1_quan_form_url_data')->where('tieid', $id)->where('quanid', $cid)->countAllResults();
			if($url_data){
				// 删除进度详情
				\Phpcmf\Service::M()->db->table('1_quan_form_url_data')->where('tieid', $id)->where('quanid', $cid)->delete();
				
				$data = [
					'code' => 1,
					'data' => '删除成功',
					'uid'=> 0,
				
				];
			} else {
				$data = [
					'code' => 0,
					'data' => '没有链接数据删除失败',
					'uid'=> 0,
				
				];
			}
			$this->_json('0', '没有链接数据删除失败',$data);
		}
	}

}
